package com.hw.one.collect.dust.job;

import com.hw.one.collect.client.ClientFactory;
import com.hw.one.collect.dust.constans.SjwConstants;
import com.hw.one.collect.dust.po.RaiseDust;
import com.hw.one.collect.dust.servcie.BaseInfoService;
import com.hw.one.collect.dust.util.WeatherUtil;
import com.hw.one.collect.util.DateUtil;
import com.hw.one.collect.util.MathCalculateUtil;
import com.mlb.analyse.webservice.DmDataInfoVO;
import com.mlb.analyse.webservice.GetRealTimePmData;
import org.apache.cxf.endpoint.Client;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/8.
 */
@Component
public class RaiseDustBaseJob {

    private static Logger log = Logger.getLogger(RaiseDustBaseJob.class);

    Map<String, String> regionIdWithOtherCode = SjwConstants.regionIdWithOtherCode;
    Map<String, String> regionIdWithName = SjwConstants.regionIdWithName;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");

    private static DecimalFormat df = new DecimalFormat("#####.####");
    private static DecimalFormat df_0 = new DecimalFormat("#####");

    @Resource
    public BaseInfoService baseInfoService;


    public synchronized List<RaiseDust> executeGetRealTimeData(String function, Object realTimePmData) throws Exception{
        List<RaiseDust> raiseDustList = new ArrayList<>();
        String createTime = sdf.format(new Date());
        Client client = ClientFactory.newInstance().getClient(SjwConstants.Monitor_URI, ClientFactory.raiseDustType);
        // 发送请求
        Object[] e = client.invoke(function, realTimePmData);
        if (e[0] instanceof List) {
            List list = (List) e[0];
            for (Object dmDataInfoVO : list) {
                if (dmDataInfoVO instanceof DmDataInfoVO) {
                    DmDataInfoVO dmDataInfoVO1 = (DmDataInfoVO) dmDataInfoVO;
                    String siteName = dmDataInfoVO1.getBSName();
                    // 如果名称为空认为是无效数据
                    if (StringUtils.isEmpty(siteName)) {
                        continue;
                    }
                    RaiseDust raiseDust = new RaiseDust();
                    raiseDust.setSITENAME(siteName);
                    String monitorTimeS = dmDataInfoVO1.getMonitorTime();
                    // 判断是否是需要的日期格式
                    if (DateUtil.isDateFormat(monitorTimeS)) {
                        raiseDust.setMONITORTIME(monitorTimeS);
                        raiseDust.setMONITORTIME2(monitorTimeS);
                    }else if (DateUtil.isDateFormat2(monitorTimeS)){
                        raiseDust.setMONITORTIME(sdf.format(sdf1.parse(monitorTimeS)));
                        raiseDust.setMONITORTIME2(sdf.format(sdf1.parse(monitorTimeS)));
                    }else {
                        // 不是合格的日期格式，跳过
                        continue;
                    }
                    // 如果监测时间大于当前时间，跳过
                    if (DateUtil.checkDate(sdf.parse(raiseDust.getMONITORTIME()), new Date())) {
                        continue;
                    }
                    String siteCode = dmDataInfoVO1.getId();
                    // 根据站点名称查询站点编码
                    List<String> siteCodeList = baseInfoService.getSiteCodeBySiteNameFromRedis(siteName);
                    // 如果有站点编码的话就保存到SITECODE，SITECODE2保存数据传回来的编码，
                    // 如果没有的话SITECODE，和SITECODE2都保存数据传回来的编码
                    if (siteCodeList != null && siteCodeList.size() ==1) {
                        raiseDust.setSITECODE(siteCodeList.get(0));
                    }else {
                        raiseDust.setSITECODE(siteCode);
                    }
                    raiseDust.setSITECODE2(siteCode);
                    if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getPM10())) {
                        raiseDust.setPM10(df_0.format(Double.valueOf(dmDataInfoVO1.getPM10())));
                    }
                    if (MathCalculateUtil.isNumeric((dmDataInfoVO1.getPM25()))) {
                        raiseDust.setPM25(df_0.format(Double.valueOf(dmDataInfoVO1.getPM25())));
                    }
                    if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getTemperature())) {
                        raiseDust.setTEMP(df.format(Double.valueOf(dmDataInfoVO1.getTemperature())));
                    }
                    if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getHumidity())) {
                        raiseDust.setHUM(df.format(Double.valueOf(dmDataInfoVO1.getHumidity())));
                    }
                    if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getNoise())) {
                        raiseDust.setNOISE(df.format(Double.valueOf(dmDataInfoVO1.getNoise())));
                    }
                    if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getWindSpeed())) {
                        raiseDust.setWS(df.format(Double.valueOf(dmDataInfoVO1.getWindSpeed())));
                    }
                    raiseDust.setWD(WeatherUtil.dealWD(dmDataInfoVO1.getDirection()));
                    raiseDust.setWD_S(dmDataInfoVO1.getDirection());

                    raiseDust.setCREATETIME(createTime);
//                    baseRedis.insert("regionCode" + siteCode, regionIdWithOtherCode.get(key));
                    // 关联oracle
                    raiseDustList.add(raiseDust);
                }
            }
        }
        return raiseDustList;
    }


    public void analysisData(List<Object> list) {
        for (Object dmDataInfoVO : list) {
            if (dmDataInfoVO instanceof DmDataInfoVO) {
                DmDataInfoVO dmDataInfoVO1 = (DmDataInfoVO) dmDataInfoVO;

                String siteName = dmDataInfoVO1.getBSName();
                if (StringUtils.isEmpty(siteName)) {
                    continue;
                }
                RaiseDust raiseDust = new RaiseDust();
                raiseDust.setSITENAME(siteName);
                String monitorTime = dmDataInfoVO1.getMonitorTime().split("\\.")[0];
                // 判断是否是需要的日期格式
                if (DateUtil.isDateFormat(monitorTime)) {
                    raiseDust.setMONITORTIME2(monitorTime);
                } else {
                    continue;
                }
                String siteCode = dmDataInfoVO1.getId();
//                siteCodeTo.add(siteCode);
                raiseDust.setSITECODE(siteCode);
//                raiseDust.setMONITORTIME(thisTime);

                if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getPM10())) {
                    raiseDust.setPM10(dmDataInfoVO1.getPM10().split("\\.")[0]);
                }
                if (MathCalculateUtil.isNumeric((dmDataInfoVO1.getPM25()))) {
                    raiseDust.setPM25(dmDataInfoVO1.getPM25().split("\\.")[0]);
                }
                if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getTemperature())) {
                    raiseDust.setTEMP(dmDataInfoVO1.getTemperature());
                }
                if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getHumidity())) {
                    raiseDust.setHUM(dmDataInfoVO1.getHumidity());
                }
                if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getNoise())) {
                    raiseDust.setNOISE(dmDataInfoVO1.getNoise());
                }
                if (MathCalculateUtil.isNumeric(dmDataInfoVO1.getWindSpeed())) {
                    raiseDust.setWS(dmDataInfoVO1.getWindSpeed());
                }

                raiseDust.setWD(WeatherUtil.dealWD(dmDataInfoVO1.getDirection()));
                raiseDust.setWD_S(dmDataInfoVO1.getDirection());

//                raiseDust.setCREATETIME(createTime);
//                baseRedis.insert("regionCode" + siteCode, regionIdWithOtherCode.get(key));
//                 关联oracle
//                raiseDustList.add(raiseDust);
            }
        }
    }
}
